/**
 * 消费习惯管理初始化
 */
var ConsumptionHabits = {
    id: "ConsumptionHabitsTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
ConsumptionHabits.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '', field: 'id', visible: false, align: 'center', valign: 'middle', width: '50px'},
        {title: '消费习惯', field: 'consumptionHabits', align: 'center', valign: 'middle', sortable: true},
        {title: '排序',visible: false, field: 'num', align: 'center', valign: 'middle', sortable: true},
        {title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true}];
};

ConsumptionHabits.check = function () { // 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        ConsumptionHabits.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加消费习惯
 */
ConsumptionHabits.openAddConsumptionHabits = function () {
    var index = layer.open({
        type: 2,
        title: '添加消费习惯',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consumptionHabits/consumptionHabits_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看消费习惯详情
 */
ConsumptionHabits.openConsumptionHabitsDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '消费习惯详情',
            area: ['90%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/consumptionHabits/consumptionHabits_update/' + ConsumptionHabits.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除消费习惯
 */
ConsumptionHabits.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/consumptionHabits/delete/" + this.seItem.id, function (data) {
            Feng.success("删除成功!");
            ConsumptionHabits.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
};


/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
ConsumptionHabits.formParams = function () {
    var queryData = {};
    queryData['consumptionHabits'] = $("#consumptionHabits").val();
    return queryData;
}

ConsumptionHabits.resetSearch = function () {
    $("#consumptionHabits").val("");
    ConsumptionHabits.search();
};

/**
 * 查询列表
 */
ConsumptionHabits.search = function () {
    ConsumptionHabits.table.refresh({query: ConsumptionHabits.formParams()});
};

$(function () {
    var defaultColunms = ConsumptionHabits.initColumn();
    var table = new BSTable(ConsumptionHabits.id, "/consumptionHabits/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(ConsumptionHabits.formParams());
    ConsumptionHabits.table = table.init();
    ConsumptionHabits.table = table;
});
